package scau.erps.util;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

import scau.erps.entity.SystemOrder;
public class OrderGenerator {
	
	/**
	 * 订单生成器
	 * @param year 年
	 * @param product 产品
	 * @param market 市场
	 * @param quantity 订单数量
	 * @param price 参考总价 
	 * @param total 订单总数
	 * @param max 单个订单最大数量
	 * @return
	 */
	public static List<SystemOrder> generateOrder(String year,String product,String market, int quantity, int total, int max, double price){
		List<SystemOrder> list = new ArrayList<SystemOrder>();
		int[] quantitys = getOrderQuantity(quantity,total,max);
		String[] quarters = getQuarter(quantity);
		String[] isos = getIso(quantity,market);
		double[] prices = getPrice(quantity,price);
		for (int i = 0; i < quantity; i++) {
			SystemOrder order = new SystemOrder();
			String deliver = year+getDeliverTime(quantitys[i]);			
			order.setYear(year);
			order.setProduct(product);
			order.setMarket(market);
			order.setQuantity(quantitys[i]);
			order.setPrice(Double.parseDouble(new DecimalFormat("#.00").format(prices[i]*quantitys[i]))); //得到订单总价
			order.setQuarter(quarters[i]);
			order.setDeliver(deliver);
			order.setIso(isos[i]);
			list.add(order);
		}		
		return list;
	}
	/**
	 * 得到订单数
	 * @param n
	 * @param m
	 * @return
	 */
	public static int[] getOrderQuantity(int n, int m, int z){		
		int[] a = new int[n];
		Random random=new Random();
		int total = m;
		while(m>0){
			int i=random.nextInt(n);
			if( a[i]<z){				
				a[i]++;				
				m--;				
			}	
			if (m==0) {
				if (check(a,z)==false) {
					m=total;
					a=new int[n];
				}
			}	
		}
		return a;		
	}
	/**
	 * 检验订单分布是否合理
	 * @param a
	 * @param z
	 * @return
	 */
	public static boolean check(int[] a,int z){
		boolean flag = true;
		for (int i = 0; i < a.length; i++) {
			if(a[i] == 0){flag = false;}			
		}
		HashSet<Integer> set=new HashSet<Integer>();
		for(int i = 0; i < a.length; i++){
			set.add(a[i]);
		}
		if(set.size()<z-1){flag = false;}		
		return flag;		
	}
	/**
	 * 得到账期
	 * @param n
	 * @return
	 */
	public static String[] getQuarter(int n){
		String[] a = new String[n];
		Random random=new Random();		
		for (int j = 0; j < a.length; j++) {
			int i=random.nextInt(100);
			if(i>=0 && i<=6){
				a[j] = "现金";
			} 
			else if(i>=7 && i<=40){
				a[j] = "1Q";
			} 
			else if(i>=41 && i<=70){
				a[j] = "2Q";
			}
			else if(i>=71 && i<=91){
				a[j] = "3Q";
			}
			else if(i>=92 && i<=99){
				a[j] = "4Q";
			}
		}
		return a;		
	}
	/**
	 * 得到交货期
	 * @param quantity
	 * @return
	 */
	public static String getDeliverTime(int quantity){
		Random random=new Random();
		int i =random.nextInt(3);
		if(quantity>=1 && quantity<=3){
			i = i+1; 
		} 
		else if (quantity>=4 && quantity<=6){
			i = i+2; 
		}
		String diliverTime = "Q"+i;
		return diliverTime;
	}
	
	
	
	/**
	 * 得到ISO
	 * @param n
	 * @return
	 */
	public static String[] getIso(int n, String market) {		
		String[] iso = new String[n];
		Random random=new Random();
		if("区域".equals(market)){
			for (int i = 0; i < iso.length; i++) {
				int r = random.nextInt(10);
				if(r==0 || r==1){
					iso[i] = "ISO9000";  //国内市场1/5的订单需要ISO9000认证
				}					
			}
		}
		else if("国内".equals(market)){
			for (int i = 0; i < iso.length; i++) {
				int r = random.nextInt(10);
				if(r==0 || r==1){
					iso[i] = "ISO9000"; //国内市场1/5的订单需要ISO9000认证
				} else if(r==9){
					iso[i] = "ISO14000";  //国内市场1/10的订单需要ISO14000认证
				}					
			}
			
		}
		else if("亚洲".equals(market)){
			for (int i = 0; i < iso.length; i++) {
				int r = random.nextInt(10);
				if(r==0 || r==1){
					iso[i] = "ISO9000";  //亚洲市场1/5的订单需要ISO9000认证
				} else if(r==8 || r==9){
					iso[i] = "ISO14000";  //亚洲市场1/5的订单需要ISO140000认证
				}					
			}
			
		}
		else if("国际".equals(market)){
			for (int i = 0; i < iso.length; i++) {
				int r = random.nextInt(10);
				if(r==0){
					iso[i] = "ISO14000";  //国际市场1/5的订单需要ISO14000认证
				}					
			}
		}
		return iso;		
	}
	/**
	 * 得到参考价格上下浮动10%范围内价格
	 * @param quantity
	 * @param price
	 * @return
	 */
	public static double[] getPrice(int quantity, double price){
		double[] prices = new double[quantity];
		Random random=new Random();
		int max=(int) ((price*11));
        int min=(int) ((price*9));
		for (int i = 0; i < prices.length; i++) {			
	        int s = random.nextInt(max)%(max-min+1) + min;
			prices[i] = s*0.1;
		}
		return prices;
	}
}

